use std::num::ParseIntError;

fn main() {
    println!("Hello, world!");
    let numbers = vec!["23", "46", "18"];
    let empty = vec![];
    let strings = vec!["tofu", "46", "18"];

    println!("the first doubled is {:?}", double_first_2(&numbers));
    println!("the first doubled is {:?}", double_first_2(&empty));
    println!("the first doubled is {:?}", double_first_2(&strings));

    println!("the first doubled is {:?}", double_first_1(&numbers));
    println!("the first doubled is {:?}", double_first_1(&empty));
    println!("the first doubled is {:?}", double_first_1(&strings));
}

fn double_first_1(vec: &Vec<&str>) -> Option<Result<i32, ParseIntError>> {
    vec.first().map(|f| f.parse::<i32>().map(|n| n * 2))
}
fn double_first_2(vec: &Vec<&str>) -> Result<Option<i32>, ParseIntError> {
    let opt = vec.first().map(|f| f.parse::<i32>().map(|n| n * 2));
    opt.map_or(Ok(None), |r| r.map(Some))
}
